package com.glory.wxmemsicpda.activity

import android.os.Bundle
import android.text.TextUtils
import androidx.databinding.DataBindingUtil
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.CheckSheetBean
import com.glory.wxmemsicpda.bean.SheetInfoBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.databinding.ActivityCheckInspectionBinding
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.JsonObject
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils
import kotlinx.android.synthetic.main.activity_check_inspection.*
import kotlinx.android.synthetic.main.activity_check_inspection.etEqpWork
import kotlinx.android.synthetic.main.activity_check_inspection.llMain

/*
*   录入检查单(巡检)
* */
class CheckInspectionActivity : BaseDialogActivity() {

    private var mCheckSheetBean: CheckSheetBean = CheckSheetBean()
    private var mObjectRRN = ""
    private var viewDataBinding: ActivityCheckInspectionBinding? = null

    override fun getLayoutResId(): Int {
        return R.layout.activity_check_inspection
    }

    override fun initView() {
        viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_check_inspection)

        setTitle("巡检")

        freeUI_titleView.setRightListener("下一步") {

//            if (mCheckSheetBean.sheetlist.size <= 0) {
//                showToast("未获取到点检数据", false);
//                return@setRightListener
//            }

            val bundle = Bundle()
            bundle.putSerializable("SHEETDATA", mCheckSheetBean)
//            bundle.putString("OBJECTRRN", mObjectRRN)
            bundle.putString("OBJECTRRN", mObjectRRN)
            bundle.putString("CHECKTIME", "巡检")
            FreeApi_Utils.skipWithExtraIntent(
                "header",
                bundle,
                this,
                CheckDetailsActivity::class.java
            )
            finish()
        }

        val eqpId = intent.extras?.getString("EQPID")
        mCheckSheetBean.equipmentid = eqpId
        mObjectRRN = intent.extras?.getString("OBJECTRRN").toString()

        //如果是从点检履历界面跳转而来就屏蔽该编辑框
        if (!TextUtils.isEmpty(eqpId)){
            etEqpWork.isEnabled = false
        }

        //region 测试
//        mCheckSheetBean.customercode = "111"
//        mCheckSheetBean.packagetype = "111"
//        mCheckSheetBean.partname = "111"
//        mCheckSheetBean.sheettime = "111"
//        mCheckSheetBean.shift = "111"
//
//        var mutableListOf: MutableList<SheetInfoBean> = mutableListOf()
//        var sheetInfoBean = SheetInfoBean()
//
//        var mutableListOf1: MutableList<SheetInfoBean.SheetCheckTableLineEntity> = mutableListOf()
//        var sheetCheckTableLineEntity = SheetInfoBean.SheetCheckTableLineEntity()
//        sheetCheckTableLineEntity.reserveD2 = "参数1"
//        sheetCheckTableLineEntity.maxvalue = "10"
//        sheetCheckTableLineEntity.minvalue = "1"
//        sheetCheckTableLineEntity.datatype = "NUM"
//        sheetCheckTableLineEntity.name = "名字1"
//        sheetCheckTableLineEntity.description = "描述1"
//        sheetCheckTableLineEntity.value = "5"
//        mutableListOf1.add(sheetCheckTableLineEntity)
//
//        var sheetCheckTableLineEntity1 = SheetInfoBean.SheetCheckTableLineEntity()
//        sheetCheckTableLineEntity1.reserveD2 = "参数2"
//        sheetCheckTableLineEntity1.maxvalue = "10"
//        sheetCheckTableLineEntity1.minvalue = "1"
//        sheetCheckTableLineEntity1.datatype = "NUM"
//        sheetCheckTableLineEntity1.name = "名字2"
//        sheetCheckTableLineEntity1.description = "描述2"
//        sheetCheckTableLineEntity1.value = "11"
//
//        mutableListOf1.add(sheetCheckTableLineEntity1)
//
//        sheetInfoBean.sheetchecktablelines = mutableListOf1
//
//        mutableListOf.add(sheetInfoBean)
//
//        mCheckSheetBean.sheetlist = mutableListOf

        //endregion

        //初始化编辑框
        initEdit(etEqpWork, object : editTextListener {
            override fun onTextChangeListener(s: String) {
                //region 测试
//                showToast("获取设备信息成功", true)
                //endregion
                requestCheckInfo(s)
            }
        })

    }

    override fun initData() {
        viewDataBinding?.checkSheetBean = mCheckSheetBean
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity("header", llMain)
    }

    //region 接口调用
    private fun requestCheckInfo(s: String) {

        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "QUERY")//查询方式 QUERY 按条件查询
        map.addProperty("EQUIPMENTID", s)//设备编号
        map.addProperty("CHECKSHEETDATARRN", mObjectRRN)//点检单号
        map.addProperty("CHECKTIMING", "Daily_Inspect")//检查时机 Daily_Inspect 巡检
        NetUtils.requestMesPosNet(this,
            "CHECKSHEET", "MEMSIC.CHECKSHEET",
            map,
            object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<CheckSheetBean?>?>(
                this
            ) {
                override fun onSuccess(response: Response<BaseResponseBean<CheckSheetBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(true, supportFragmentManager)
                    ) {
                        mCheckSheetBean = response.body()!!.response.body!!
                        var sheetInfoBean = mCheckSheetBean.sheetlist[0]
                        if (sheetInfoBean == null) {
                            sheetInfoBean = SheetInfoBean()
                        }
                        viewDataBinding?.sheetInfoBean = sheetInfoBean
                        viewDataBinding?.checkSheetBean = mCheckSheetBean
                    }
                }

                override fun onError(response: Response<BaseResponseBean<CheckSheetBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "CHECKSHEET")
                }
            })
    }


    //endregion

}